#include "diaaddperson.h"
#include "ui_diaaddperson.h"
#include <QDebug>
#include <QDate>


DiaAddPerson::DiaAddPerson(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DiaAddPerson)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("Add person"));
    this->actSetX = new QAction(tr("Set"),this);
    this->actClearX = new QAction(tr("Clear"),this);

    connect(this->actSetX,SIGNAL(triggered()),this,SLOT(slotSetX()));
    connect(this->actClearX,SIGNAL(triggered()),this,SLOT(slotClearX()));

    this->ui->twg_calendar->installEventFilter(this);

    connect(this->ui->twg_calendar,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(slotDoubleClick(int,int)));

}

DiaAddPerson::~DiaAddPerson()
{
    delete ui;
}

void DiaAddPerson::slotTestaccept(){
    int a = this->ui->le_fio->text().size();
    int b = this->ui->le_profesio->text().size();
    int c = this->ui->sb_tablenumber->value();
    if( a > 0 and b > 0 and c > 0 ){
        this->ui->btn_accept->setEnabled(true);
    }
}

QString DiaAddPerson::getFio(){
    return this->ui->le_fio->text();
}

QString DiaAddPerson::getProfesio(){
    return this->ui->le_profesio->text();
}

QString DiaAddPerson::getTableNumber(){
    return QString::number(this->ui->sb_tablenumber->value());

}
void DiaAddPerson::insertFio(QString txt){
    this->setWindowTitle(tr("Edit person"));
    this->ui->le_fio->setText(txt);

}
void DiaAddPerson::insertProfesio(QString txt){
    this->ui->le_profesio->setText(txt);

}
void DiaAddPerson::insertTableNumber(QString txt){
    this->ui->sb_tablenumber->setValue(txt.toInt());

}
void DiaAddPerson::InitCalendar(QDate dt){

    int rc = (dt.daysInMonth() + dt.dayOfWeek() - 1)/7;
    if((dt.daysInMonth() + dt.dayOfWeek() - 1)%7 > 0)
       rc++;
    qDebug()<<"dayinmonth"<<dt.daysInMonth()<<"dayofWeek"<<dt.dayOfWeek()<<"rc"<<rc<<"%7"<<dt.daysInMonth()%7;
    this->ui->twg_calendar->setColumnCount(7);
    QTableWidgetItem * item = new QTableWidgetItem(tr("Mo"));
    this->ui->twg_calendar->setHorizontalHeaderItem(0,item);
    item = new QTableWidgetItem(tr("Tu"));
    this->ui->twg_calendar->setHorizontalHeaderItem(1,item);
    item = new QTableWidgetItem(tr("We"));
    this->ui->twg_calendar->setHorizontalHeaderItem(2,item);
    item = new QTableWidgetItem(tr("Th"));
    this->ui->twg_calendar->setHorizontalHeaderItem(3,item);
    item = new QTableWidgetItem(tr("Fr"));
    this->ui->twg_calendar->setHorizontalHeaderItem(4,item);
    item = new QTableWidgetItem(tr("Sa"));
    this->ui->twg_calendar->setHorizontalHeaderItem(5,item);
    item = new QTableWidgetItem(tr("Su"));
    this->ui->twg_calendar->setHorizontalHeaderItem(6,item);
    int dayCount = 1;
    for(int i = 1 ;i <= rc ; i++){
        this->ui->twg_calendar->insertRow(this->ui->twg_calendar->rowCount());
        for (int j = 0; j < this->ui->twg_calendar->columnCount(); j++){
           if((dayCount < dt.dayOfWeek()) or (dayCount > dt.daysInMonth() + dt.dayOfWeek() -1)){
               item = new QTableWidgetItem("");
               item->setBackgroundColor("Gray");
               this->ui->twg_calendar->setItem(this->ui->twg_calendar->rowCount()-1,j,item);
            }
            else {
               item = new QTableWidgetItem(QString::number(dayCount - dt.dayOfWeek() + 1));
               item->setTextAlignment(Qt::AlignHCenter);
            //item = new QTableWidgetItem(QString::number((j + 1 + 7 * (i - 1)) - dt.dayOfWeek() + 1));
            this->ui->twg_calendar->setItem(this->ui->twg_calendar->rowCount()-1,j,item);

            }
           dayCount++;
        }
    }
    this->ui->twg_calendar->resizeColumnsToContents();
    this->ui->twg_calendar->resizeRowsToContents();
}

bool DiaAddPerson::eventFilter(QObject * obj, QEvent * event){
if(event->type() == QEvent::ContextMenu and obj->objectName() == "twg_calendar"){
    QMenu menu(this);
    menu.addAction(this->actSetX);
    menu.addAction(this->actClearX);
    menu.exec(QCursor::pos());
    return true;
}else{
    return QObject::eventFilter(obj,event);
}
}

void DiaAddPerson::slotSetX(){

    qDebug()<<this->ui->twg_calendar->selectedItems().count();
    DiaSetX ddx;
    if(ddx.exec()){
    for(int i = 0 ; i < this->ui->twg_calendar->selectedItems().count() ; i++){

        QString b = this->ui->twg_calendar->selectedItems().at(i)->text();
        QString a = b.split("\n").at(0);
        if(!a.isEmpty()){
            a.append("\n");
            a.append(ddx.getTD());
            a.append("\n");
            a.append(ddx.getTime());          
            this->ui->twg_calendar->selectedItems().at(i)->setText(a);
        }
        }
    }
    this->ui->twg_calendar->resizeColumnsToContents();
    this->ui->twg_calendar->resizeRowsToContents();

}

void DiaAddPerson::slotDoubleClick(int row, int column){
    qDebug()<<"row "<<row<<" ; column "<<column;
    this->slotSetX();

}


void DiaAddPerson::slotClearX(){
    for(int i = 0 ; i < this->ui->twg_calendar->selectedItems().count() ; i++){

        QString b = this->ui->twg_calendar->selectedItems().at(i)->text();
        qDebug()<<"bbbbb "<<b;
        if(!b.isEmpty()){
            QString a = b.split("\n").at(0);
            if(!a.isEmpty()){
                this->ui->twg_calendar->selectedItems().at(i)->setText(a);
            }
        }
    }
    this->ui->twg_calendar->resizeColumnsToContents();
    this->ui->twg_calendar->resizeRowsToContents();
    qDebug()<<"end";
}

QStringList DiaAddPerson::getCalendar(){
    QStringList ret;

    for(int i = 0 ; i < this->ui->twg_calendar->rowCount() ; i++){
        for(int j = 0 ; j < this->ui->twg_calendar->columnCount() ; j++){
        QString b = this->ui->twg_calendar->item(i,j)->text();
        QStringList bb = b.split("\n");
        QString a = bb.at(0);
        if(!a.isEmpty()){
            QString c;
            if(bb.size()==3){
                c.append(bb.at(1));
                c.append(",");
                c.append(bb.at(2));

            }
            ret.append(c);
        }
    }
    }

    return ret;
}
void DiaAddPerson::fromXmlToCalendar(QStringList list){


    int  k = 0;
    for(int i = 0;i<this->ui->twg_calendar->rowCount();i++){
        for(int j = 0;j <  this->ui->twg_calendar->columnCount();j++){
            QString b = this->ui->twg_calendar->item(i,j)->text();
            QString a = b.split("\n").at(0);
            k++;
            if(!a.isEmpty()){
                int m =(j+1+7*(i)-k);
                a.append("\n");
                a.append(list.at(m).split(",").at(0));
                a.append("\n");
                a.append(list.at(m).split(",").at(1));

                this->ui->twg_calendar->item(i,j)->setText(a);
                k--;
            }
            }

        }
    this->ui->twg_calendar->resizeColumnsToContents();
    this->ui->twg_calendar->resizeRowsToContents();
}


void DiaAddPerson::fromCalendarToXml(QStringList list){


}
